import { window, Range, workspace, TextDocumentChangeEvent, Disposable, commands } from "vscode";

export class 输入法上屏命令_ {
    public static readonly ID = 'ctsscript.输入法上屏命令_';
    public readonly ID = 输入法上屏命令_.ID;
    清理: Disposable;
    需要删除上屏字符 = false;

    constructor() {
        let 处理垃圾: Disposable[] = [];
        workspace.onDidChangeTextDocument(this.监听输入, this, 处理垃圾);
        this.清理 = Disposable.from(...处理垃圾);
    }

    public async execute(文本: string, 范围: Range) {
        if (文本) {
            commands.executeCommand("vsc.保存用户词典", 文本);
        }
        if (范围 && this.需要删除上屏字符) {
            window.activeTextEditor!.edit(编辑 => {
                编辑.delete(范围);
            });
            this.需要删除上屏字符 = false;
        }
    }

    dispose() {
        this.清理.dispose();
    }

    监听输入(事件: TextDocumentChangeEvent) {
        if (/^ +$/g.test(事件.contentChanges[0].text)) {
            this.需要删除上屏字符 = true;
        }
    }
}
